package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.domain.JingfeiPici;
import com.ruoyi.system.domain.ZhuanjiaBiandongDetail;
import com.ruoyi.system.service.IDeptidOlddeptidService;
import com.ruoyi.system.service.IJingfeiPiciService;
import com.ruoyi.web.controller.tool.JsonResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ZhuanjiaBiandongQueren;
import com.ruoyi.system.service.IZhuanjiaBiandongQuerenService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2023-07-10
 */
@Controller
@RequestMapping("/system/queren")
public class ZhuanjiaBiandongQuerenController extends BaseController
{
    private String prefix = "system/queren";

    @Autowired
    private IZhuanjiaBiandongQuerenService zhuanjiaBiandongQuerenService;
    @Autowired
    private IDeptidOlddeptidService deptidOlddeptidService;
    @Autowired
    private IJingfeiPiciService jingfeiPiciService;

    @RequiresPermissions("system:queren:view")
    @GetMapping()
    public String queren()
    {
        return prefix + "/queren";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("system:queren:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ZhuanjiaBiandongQueren zhuanjiaBiandongQueren)
    {
        startPage();
        List<ZhuanjiaBiandongQueren> list = zhuanjiaBiandongQuerenService.selectZhuanjiaBiandongQuerenList(zhuanjiaBiandongQueren);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("system:queren:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ZhuanjiaBiandongQueren zhuanjiaBiandongQueren)
    {
        List<ZhuanjiaBiandongQueren> list = zhuanjiaBiandongQuerenService.selectZhuanjiaBiandongQuerenList(zhuanjiaBiandongQueren);
        ExcelUtil<ZhuanjiaBiandongQueren> util = new ExcelUtil<ZhuanjiaBiandongQueren>(ZhuanjiaBiandongQueren.class);
        return util.exportExcel(list, "【请填写功能名称】数据");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("system:queren:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZhuanjiaBiandongQueren zhuanjiaBiandongQueren)
    {
        return toAjax(zhuanjiaBiandongQuerenService.insertZhuanjiaBiandongQueren(zhuanjiaBiandongQueren));
    }

    /**
     * 修改【请填写功能名称】
     */
    @RequiresPermissions("system:queren:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        ZhuanjiaBiandongQueren zhuanjiaBiandongQueren = zhuanjiaBiandongQuerenService.selectZhuanjiaBiandongQuerenById(id);
        mmap.put("zhuanjiaBiandongQueren", zhuanjiaBiandongQueren);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("system:queren:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZhuanjiaBiandongQueren zhuanjiaBiandongQueren)
    {
        return toAjax(zhuanjiaBiandongQuerenService.updateZhuanjiaBiandongQueren(zhuanjiaBiandongQueren));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("system:queren:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(zhuanjiaBiandongQuerenService.deleteZhuanjiaBiandongQuerenByIds(ids));
    }

    @PostMapping("finddanweiquerenbyparentid")
    @ResponseBody
    public JsonResult<?> finddanweiquerenbyparentid(@RequestParam("deptid")String ppDeptId,@RequestParam("jingfeipiciid")String ppJingfeipiciId,@RequestParam("deptname")String ppDeptName,@RequestParam("shifouqueren")String ppShifouqueren){

        List<Map<String,Object>> mmList = zhuanjiaBiandongQuerenService.findQuerenByParentid(ppDeptId,ppJingfeipiciId,ppDeptName,ppShifouqueren);

        return JsonResult.getSuccessResult(mmList);
    }

    @PostMapping("/queren")
    @ResponseBody
    public JsonResult<?> queren(@RequestParam("deptid")String ppDeptId)
    {
        try{
            ZhuanjiaBiandongDetail zhuanjiaBiandongDetail = new ZhuanjiaBiandongDetail();

            zhuanjiaBiandongDetail.setId(UUID.randomUUID().toString());
            zhuanjiaBiandongDetail.setDeptid(ppDeptId);
            zhuanjiaBiandongDetail.setZhuanjiamingcheng("无变动确认");
            zhuanjiaBiandongDetail.setBiandongriqi(new Date());
            zhuanjiaBiandongDetail.setShenpishijian(new Date());
            zhuanjiaBiandongDetail.setBeizhu1("1");
            zhuanjiaBiandongDetail.setIsused((long)0);
            zhuanjiaBiandongDetail.setIsvalid((long)1);

            ZhuanjiaBiandongQueren mmZhuanjiabiandongQueren = new ZhuanjiaBiandongQueren();
            mmZhuanjiabiandongQueren.setQuerenshijian(new Date());
            mmZhuanjiabiandongQueren.setDeptid(deptidOlddeptidService.DeptIdToOldDeptId(ppDeptId));
            mmZhuanjiabiandongQueren.setIsvalid((long)1);

            boolean isOk = zhuanjiaBiandongQuerenService.querenZJZX(zhuanjiaBiandongDetail,mmZhuanjiabiandongQueren);

            return isOk==true?JsonResult.getSuccessResult("保存成功"):JsonResult.getErrorResult("保存失败");

        } catch (Exception e) {
            logger.error("zhuanjiabiandongqueren -> queren: "+e.getMessage());
            return JsonResult.getErrorResult("添加变动确认失败");
        }
    }

    @PostMapping("/checkqueren")
    @ResponseBody
    public JsonResult<?> checkQueren(@RequestParam("deptid")String ppDeptId)
    {
        try{

            JingfeiPici mmJingfeipici = new JingfeiPici();
            mmJingfeipici.setIsdangqianpici(1);
            mmJingfeipici.setDeptid("0");
            List<JingfeiPici> mmJingfeipiciList = jingfeiPiciService.selectJingfeiPiciList(mmJingfeipici);
            for (int i=0;i<mmJingfeipiciList.size();i++){
                String mmPiciId = mmJingfeipiciList.get(i).getId();
                ZhuanjiaBiandongQueren tempZhuanjiabiandongQueren = new ZhuanjiaBiandongQueren();
                tempZhuanjiabiandongQueren.setJingfeipiciid(mmPiciId);
                tempZhuanjiabiandongQueren.setDeptid(deptidOlddeptidService.DeptIdToOldDeptId(ppDeptId));
                List<ZhuanjiaBiandongQueren> mmTempList = zhuanjiaBiandongQuerenService.selectZhuanjiaBiandongQuerenList(tempZhuanjiabiandongQueren);
                if (mmTempList.size()!=1){
                    return JsonResult.getSuccessResult("0");
                }
            }

            return JsonResult.getSuccessResult("1");

        } catch (Exception e) {
            logger.error("zhuanjiabiandongqueren -> queren: "+e.getMessage());
            return JsonResult.getErrorResult("添加变动确认失败");
        }
    }
}
